var caigoudanObj={
	inits:function(){
		this.ajax();
		this.checkFun();//全选／全不选
		this.addOrRed();//加一或减一
		this.removeFun();//删除一行商品
		this.TotalFun(); //计算选中商品的价格和数量
		this.createTempOrderFun();  //确定订单
	},
	Interface:{
		caigoudanlist:"http://ww.yc630.com/api/index.php/Buyer/Order/caigoudanlist",//采购单列表
		createTempOrder:"http://ww.yc630.com/api/index.php/Buyer/Order/createTempOrder",//生成预订单－下面确定按钮
		isLogin:"http://ww.yc630.com/api/index.php/Buyer/Login/isLogin",  //是否登录
		delcaigoudan:"http://ww.yc630.com/api/index.php/Buyer/Order/delcaigoudan"//删除
	},
	ajax:function(){
			common.ajaxFun(caigoudanObj.Interface.isLogin,"POST",{token:token},function(res){
			if (res.code==1) {
				token=common.getCookie("Temptoken");
			}else if (res.code==0) {
				token=common.getCookie("token");
			};
		

		common.ajaxFun(caigoudanObj.Interface.caigoudanlist,"POST",{token:token,page:1,pagelimit:15},caigoudanObj.cgdFun); //采购单
			});	
	},
	cgdFun:function(res){
		
		var str="";
		$.each(res.data.list,function(i,v){
			str+='<div class="commodityGroup">'+
					'<div class="BrandName">'+
						'<input type="checkbox" id="checkGroup'+i+'" class="check checkGroup">'+
						'<label for="checkGroup'+i+'" class="checkLabel">'+v.seller_name+'</label>'+
					'</div>'+
					'<div class="commodityColumn">';
					$.each(v.goods,function(a,b){
						var goods_attrs=b.goods_attr.split("；");
						str+='<div class="row row'+a+'">'
								str+='<div class="td first">'
									str+='<input type="checkbox" id="check'+i+'_'+a+'" class="check listCheck" valueId='+b.id+' goods_nu='+b.goods_nu+' total_money='+b.total_money+'>'
									str+='<label for="check'+i+'_'+a+'" class="checkLabel check"></label>'
									str+='<div class="commodityImg"><img src='+b.goods_image+' alt=""></div>'
									str+='<div class="commodityIntroduce">'+b.goods_name+'</div>'
								str+='</div>'
								str+='<div class="td second">'
									str+='<div class="size">'
									$.each(goods_attrs,function(c,d){
										if (d=="统一定价：统一") {
											str+=""
										}else{
											str+='<div>'+d+'</div>'
										};
									});
									str+='</div>'
								str+='</div>'
								str+='<div class="td third">'
									str+='<div class="commodityPrice">¥<span>'+b.goods_price+'</span>元</div>'
								str+='</div>'
								str+='<div class="td fourth">'
									str+='<div class="commodityNum">'
										str+='<div class="reduce control">－</div>'
										str+='<input type="text" class="commodityNumber" value='+b.goods_nu+' readonly>'
										str+='<div class="add control">＋</div>'
									str+='</div>'
								str+='</div>'
								str+='<div class="td fifth">'
									str+='<div class="commoditymoney">¥'+b.total_money+'</div>'
									str+='<div class="close"></div>'
								str+='</div>'
							str+='</div>';
					});
					str+='</div></div><div class="clear"></div>';
		});

		$("#clothingList").html(str);
	},
	TotalFun:function(){
			var totNum=0,totMon=0;
			$("input.listCheck:checked").each(function() {
	           totNum+=Number($(this).attr("goods_nu"));
	           totMon+=Number($(this).attr("total_money"));
	        });
	        $("#TotalNum").html(totNum);
	        $("#TotalMon").html(totMon.toFixed(2));
	},
	checkFun:function(){
		$('.content').on("click","input[type=checkbox]",function(){
			caigoudanObj.TotalFun();
		});
		$("#checkAll").click(function(){
			caigoudanObj.check($('input[type=checkbox]'),this);
		});

		$("#clothingList").on("click",".checkGroup",function(){
			caigoudanObj.check($(this).parent(".BrandName").siblings(".commodityColumn").find("input[type=checkbox]"),this);
		})
	},
	check:function(inputs,t){
		var _this=t;
			var inputs=inputs;
			inputs.each(function(){
				this.checked=_this.checked;
			});
	},
	addOrRed:function(){
		$("#clothingList").on("click",".add",function(){
			var goods_price=$(this).parents("div.fourth").prev(".third").find("span").text();
			var valNum=$(this).prev("input.commodityNumber").val();
			valNum++;
			$(this).prev("input.commodityNumber").val(valNum);
			price=goods_price*valNum;
			$(this).parents("div.fourth").next(".fifth").children("div.commoditymoney").html("¥"+price.toFixed(2))
			$(this).parents("div.fourth").siblings(".first").children("input").attr("goods_nu",valNum);
			$(this).parents("div.fourth").siblings(".first").children("input").attr("total_money",price.toFixed(2));
				caigoudanObj.TotalFun();
		});
		$("#clothingList").on("click",".reduce",function(){
			var valNum=$(this).next("input.commodityNumber").val();
			if (valNum<=0) {
				alert("数量不能小于0");
				return false;
			}else {
				var goods_price=$(this).parents("div.fourth").prev(".third").find("span").text();
				valNum--;
				$(this).next("input.commodityNumber").val(valNum);
				price=goods_price*valNum;
				$(this).parents("div.fourth").next(".fifth").children("div.commoditymoney").html("¥"+price.toFixed(2));
				$(this).parents("div.fourth").siblings(".first").children("input").attr("goods_nu",valNum);
				$(this).parents("div.fourth").siblings(".first").children("input").attr("total_money",price.toFixed(2));
			
				caigoudanObj.TotalFun();
			}
		});
	},
	removeFun:function(){
		$("#clothingList").on("click",".close",function(){
			
			var purchase_id=$(this).parents("div.row").find("input").attr("valueid"),
			that=$(this);
			//console.log(purchase_id)
	        common.ajaxFun(caigoudanObj.Interface.delcaigoudan,"POST",{token:token,purchase_id:purchase_id},function(res){
	        	if (res.code==0) {
		        	var removeLen=that.parents("div.commodityColumn").children(".row").length-1;
		        	if (removeLen==0) {
		        		that.parents("div.commodityGroup").remove();
		        	}else {
		        	console.log(removeLen)

		        		that.parents("div.row").remove();
		        	};
	        	};
	        })

			caigoudanObj.TotalFun();
		})
	},
	createTempOrderFun:function(){
		$("#orderBtn").click(function(){
			var id="";
			$("input.listCheck:checked").each(function() {
	           id+=$(this).attr("valueid")+":"+$(this).attr("goods_nu")+":"+$(this).attr("total_money")+",";
	        });
	        if (id=="") {
	        	alert("请选择下单的商品！");
	        	return false;
	        };
	        newid=id.substring(0,id.length-1);
	        		common.ajaxFun(caigoudanObj.Interface.isLogin,"POST",{token:token},function(res){
	        		if (res.code==1) {//未登录
	        			alert("请登录！");
						common.setCookie("hrefs",window.location.href);
	        			window.location.href="./login/login.html";
	        		}else if (res.code==0) {
	        			common.ajaxFun(caigoudanObj.Interface.createTempOrder,"POST",{token:token,purchaseinfo:newid},function(res){
	        				if (res.code==0) {
	        					window.location.href="./confirmOrder.html?orderid="+res.data.orderid;
	        				}else {
	        					alert(res.msg);
	        				};
	        			}); //下面的确定按钮
	        		};
	        	});	

			
		})

	}
}
caigoudanObj.inits()